home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-9.10-netbook-remix-PL.iso / casper / filesystem.squashfs / usr / lib / python2.6 / compiler / future.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2009-11-11  |  3KB  |  84 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. '''Parser for future statements
  5.  
  6. '''
  7. from compiler import ast, walk
  8.  
  9. def is_future(stmt):
  10.     '''Return true if statement is a well-formed future statement'''
  11.     if not isinstance(stmt, ast.From):
  12.         return 0
  13.     if stmt.modname == '__future__':
  14.         return 1
  15.     return 0
  16.  
  17.  
  18. class FutureParser:
  19.     features = ('nested_scopes', 'generators', 'division', 'absolute_import', 'with_statement', 'print_function', 'unicode_literals')
  20.     
  21.     def __init__(self):
  22.         self.found = { }
  23.  
  24.     
  25.     def visitModule(self, node):
  26.         stmt = node.node
  27.         for s in stmt.nodes:
  28.             if not self.check_stmt(s):
  29.                 break
  30.                 continue
  31.         
  32.  
  33.     
  34.     def check_stmt(self, stmt):
  35.         if is_future(stmt):
  36.             for name, asname in stmt.names:
  37.                 if name in self.features:
  38.                     self.found[name] = 1
  39.                     continue
  40.                 raise SyntaxError, 'future feature %s is not defined' % name
  41.             
  42.             stmt.valid_future = 1
  43.             return 1
  44.         return 0
  45.  
  46.     
  47.     def get_features(self):
  48.         '''Return list of features enabled by future statements'''
  49.         return self.found.keys()
  50.  
  51.  
  52.  
  53. class BadFutureParser:
  54.     '''Check for invalid future statements'''
  55.     
  56.     def visitFrom(self, node):
  57.         if hasattr(node, 'valid_future'):
  58.             return None
  59.         if node.modname != '__future__':
  60.             return None
  61.         raise SyntaxError, 'invalid future statement ' + repr(node)
  62.  
  63.  
  64.  
  65. def find_futures(node):
  66.     p1 = FutureParser()
  67.     p2 = BadFutureParser()
  68.     walk(node, p1)
  69.     walk(node, p2)
  70.     return p1.get_features()
  71.  
  72. if __name__ == '__main__':
  73.     import sys
  74.     from compiler import parseFile, walk
  75.     for file in sys.argv[1:]:
  76.         print file
  77.         tree = parseFile(file)
  78.         v = FutureParser()
  79.         walk(tree, v)
  80.         print v.found
  81.         print 
  82.     
  83.  
  84.